package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.MetricPointModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 计量点业务
 * @author zhaokf
 * @date 2013-04-19
 */
public class MetricPointService extends BaseService
{
	/**
	 * 分页列出计量点
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<MetricPointModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("mp_code") != null){
				where = " AND mp_code=" + search.get("mp_code");
			}
			if (search.get("mp_name") != null){
				where = " AND mp_name LIKE '%" + search.get("mp_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<MetricPointModel> Mp = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM C_MP " + where;

		try{
			Mp = MetricPointModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Mp;
	}



	/**
	 * 增加计量点信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){

//		String no = params.get("mp_code")==null?"":params.get("mp_code").toString();
//		String name = params.get("mp_name")==null?"":params.get("mp_name").toString();
//		String line = params.get("mp_line_id")==null?"":params.get("mp_line_id").toString();
//		String tran = params.get("mp_tran_id")==null?"":params.get("mp_tran_id").toString();
//
//		String sql = "select * from c_mp where mp_code=?";
//		MetricPointModel mp = MetricPointModel.dao.findFirst(sql, no);
//		if (mp == null){
//			mp =new MetricPointModel();
//			mp.set("mp_id","C_MP_SEQ.nextval");
//			mp.set("mp_code",no);
//			mp.set("mp_name", name);
//			mp.set("mp_line_id", line);
//			mp.set("mp_tran_id", tran);
//			mp.save();
//			return true;
//		}
		Record record = new Record();
		record.set("mp_id", Db.queryBigDecimal("select c_mp_seq.nextval from dual").intValue());
		record.set("mp_code", params.get("mp_code").toString());
		record.set("mp_name", params.get("mp_name").toString());
		record.set("mp_line_id", params.get("mp_line_id").toString());
		record.set("mp_tran_id", params.get("mp_tran_id").toString());
		boolean isupadded = Db.save("c_mp", "mp_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的计量点已存在！");
	}

	/**
	 * 修改计量点信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		Record record = new Record();
		record.set("mp_id", params.get("mp_id").toString());
		record.set("mp_code", params.get("mp_code").toString());
		record.set("mp_name", params.get("mp_name").toString());
		record.set("mp_line_id", params.get("mp_line_id").toString());
		record.set("mp_tran_id", params.get("mp_tran_id").toString());
		boolean isupadded = Db.save("c_mp", "mp_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的计量点存在！");
	}

	/**
	 * 删除计量点信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("mp_code")==null?"":params.get("mp_code").toString();
		String sql = "select * from c_mp where mp_code=?";
		MetricPointModel mp = MetricPointModel.dao.findFirst(sql, no);
		if (mp != null)
		{
			mp.delete();
			return true;
		}
		return setError("您访问的计量点不存在！");
	}
}

